<?php
    if ($default_link) {
        $from = url('show search link', ['url' => $default_link->url]);
    } else {
        $from = url('show search link');
    }

    $this->layout('base.phtml', [
        'title' => _('New'),
        'canonical' => url_full('show search link'),
        'current_page' => 'search link',
        'back_options' => [
            'reset' => true,
        ],
        'has_errors' => $error || $errors,
    ]);
?>

<div class="section">
    <div class="section__title">
        <h1 id="modal-title"><?= _('New') ?></h1>
    </div>

    <form
        method="post"
        action="<?= url('search link') ?>"
        <?php if ($autosubmit): ?>
             data-controller="autosubmit"
             data-autosubmit-mode-value="timeout"
             data-autosubmit-timeout-value="0"
        <?php endif; ?>
    >
        <?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

        <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />

        <div class="form-group <?= isset($errors['url']) ? 'form-group--invalid' : '' ?>">
            <label for="url">
                <?= _('What’s the address of the link or feed?') ?>
            </label>

            <div class="form-group__stack">
                <input
                    id="url"
                    name="url"
                    type="url"
                    placeholder="https://…"
                    required
                    value="<?= $url ?>"
                    autocomplete="off"
                    aria-describedby="url-desc"
                />

                <button
                    type="submit"
                    class="button--primary no-mobile"
                    data-autosubmit-target="actionButton"
                >
                    <?= _('Search') ?>
                </button>
            </div>

            <div id="url-desc">
                <?php if (isset($errors['url'])): ?>
                    <p class="form-group__error">
                        <span class="sr-only"><?= _('Error') ?></span>
                        <?= $errors['url'] ?>
                    </p>
                <?php endif; ?>
            </div>
        </div>

        <div class="form__actions only-mobile">
            <button type="submit" class="button--primary">
                <?= _('Search') ?>
            </button>
        </div>
    </form>

    <?php if ($existing_link || $default_link): ?>
        <div class="cards cards--centered">
            <?php if ($existing_link): ?>
                <?= $this->include('links/_link.phtml', [
                    'link' => $existing_link,
                    'from' => $from,
                    'display_published_at' => false,
                    'display_notes' => true,
                    'display_edit' => true,
                    'display_repair' => true,
                    'display_delete' => true,
                    'display_read_later' => 'auto',
                    'display_mark_as_read' => 'auto',
                ]); ?>
            <?php elseif ($default_link && !($default_link->isFeedUrl() && $feeds)): ?>
                <?= $this->include('links/_link.phtml', [
                    'link' => $default_link,
                    'from' => $from,
                    'display_published_at' => false,
                    'display_read_later' => 'auto',
                    'display_mark_as_read' => 'auto',
                ]); ?>
            <?php endif; ?>

            <?php foreach ($feeds as $feed): ?>
                <div class="card card--illustrated" style="background-image: url('<?= url_media('covers', $feed->image_filename, 'collection-card.svg') ?>');">
                    <a class="card__body card__body--larger" href="<?= url('collection', ['id' => $feed->id]) ?>">
                        <div class="card__title card__title-ellipsis">
                            <?= protect($feed->name) ?>
                        </div>

                        <p class="card__text card__text--oneline">
                            <?php
                                $source = _f('Feed %s', protect($feed->feedWebsite()));

                                if ($feed->number_links === 0):
                                    $number_links = _('no links');
                                else:
                                    $number_links = _nf('%s link', '%s links', $feed->number_links, format_number($feed->number_links));
                                endif;
                            ?>

                            <span class="card__ellipsis"><?= $source ?></span>&nbsp;·&nbsp;<?= $number_links ?>

                            <p class="card__text" title="<?= _('Publication frequency') ?>">
                                <?= icon('line-chart') ?>
                                <?= format_publication_frequency($feed->publication_frequency_per_year) ?>
                            </p>
                        </p>
                    </a>

                    <div class="card__footer card__footer--centered">
                        <?php if ($current_user->isFollowing($feed->id)): ?>
                            <form data-turbo-preserve-scroll method="post" action="<?= url('unfollow collection', ['id' => $feed->id]) ?>">
                                <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                <input type="hidden" name="from" value="<?= $from ?>" />
                                <button>
                                    <?= icon('feed-stop') ?>
                                    <?= _('Unfollow') ?>
                                </button>
                            </form>
                        <?php else: ?>
                            <form data-turbo-preserve-scroll method="post" action="<?= url('follow collection', ['id' => $feed->id]) ?>">
                                <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
                                <input type="hidden" name="from" value="<?= $from ?>" />
                                <button>
                                    <?= icon('feed') ?>
                                    <?= _('Follow') ?>
                                </button>
                            </form>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>

    <?php if (!$existing_link && !$default_link && !$feeds): ?>
        <img
            class="illustration illustration--centered"
            alt=""
            src="<?= url_static('illustrations/location-search.svg') ?>"
            height="250"
        />
    <?php endif; ?>
</div>
